﻿using System;
using RpcHelper;
using RpcModularModel.Resource;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class IdentityProwerService : IIdentityProwerService
        {
                private readonly IIdentityProwerCollect _IdentityPrower;
                private readonly IServerTypeCollect _ServerType;
                public IdentityProwerService(IIdentityProwerCollect identityPrower, IServerTypeCollect serverType)
                {
                        this._ServerType = serverType;
                        this._IdentityPrower = identityPrower;
                }

                public string[] GetProwers(Guid appId, string systemType, ResourceType type)
                {
                        return this._IdentityPrower.GetProwers(appId, systemType, type);
                }
                public string[] GetSystemType(Guid appId)
                {
                        return this._IdentityPrower.GetSystemType(appId);
                }

                public void Sync(IdentityProwerList add)
                {
                        string[] adds = add.Values.Distinct(a => a.ResourceType != ResourceType.RPC节点, a => a.SystemType);
                        string[] sysType = add.Values.Distinct(a => a.ResourceType == ResourceType.RPC节点, a => a.SystemType);
                        if (sysType.Length != adds.Length)
                        {
                                adds = adds.Remove(sysType);
                                if (adds.Length > 0)
                                {
                                        add.Values = add.Values.Add(adds.ConvertAll(a => new IdentityProwerVal
                                        {
                                                Value = string.Empty,
                                                SystemType = a,
                                                ResourceType = ResourceType.RPC节点
                                        }));
                                }
                        }
                        this._IdentityPrower.Sync(add);
                }
        }
}
